package com.shopping.dao;

import com.shopping.pojo.Record;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Mapper
public interface RecordDao {

    String TABLE_NAME = "record";
    String INSERT_FIELDS = "user_id, trade_id, time";
    String SELECT_FIELDS = "id, user_id userId, trade_id tradeId, time";

    @Insert({"insert into ", TABLE_NAME, " (", INSERT_FIELDS, ") values(#{userId}, #{tradeId}, #{time})"})
    int insert(Record record);

    @Delete({"delete from ", TABLE_NAME, " where id=#{id}"})
    int delete(@Param("id") int id);

    @Delete({"delete from ", TABLE_NAME, " where user_id=#{userId}"})
    int deleteAll(@Param("userId") int userId);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where id=#{id}"})
    Record selectRecord(@Param("id") int id);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where user_id=#{userId} and trade_id=#{tradeId}"})
    Record select(@Param("userId") int userId, @Param("tradeId") int tradeId);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where user_id=#{userId}"})
    List<Record> selectRecords(@Param("userId") int userId);

    @Update({"update ", TABLE_NAME, " set time=#{time} where user_id=#{userId} and trade_id=#{tradeId}"})
    int updateTime(@Param("time") Date time, @Param("userId") int userId, @Param("tradeId") int tradeId);

}
